package com.cnswhy.cloud.server.core.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.common.entity.enumeration.IotType;
import com.cnswhy.cloud.server.core.dao.IotTerminalInfoMapper;
import com.cnswhy.cloud.server.core.entity.IotTerminalInfo;
import com.cnswhy.cloud.server.core.service.IotTerminalInfoService;
import com.cnswhy.cloud.common.core.base.service.SuperServiceImpl;

import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.merchant.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 业务实现类
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2021-03-24
 */
@Slf4j
@Service

public class IotTerminalInfoServiceImpl extends SuperServiceImpl<IotTerminalInfoMapper, IotTerminalInfo> implements IotTerminalInfoService {
    @Autowired
    MerchantService merchantService;
    @Autowired
    CustomerService customerService;
    @Override
    public IotTerminalInfo queryTerminal(IotType iotType, String terminalId,Long customerId) {
        IotTerminalInfo iotTerminalInfo = getOne(
                Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getTerminalId, terminalId)
                        .eq(IotTerminalInfo::getIotType, iotType)
        );
        iotTerminalInfo.setMerchantFlag(false);
        iotTerminalInfo.setCustomerFlag(false);
        iotTerminalInfo.setSelfFlag(false);

        if(iotTerminalInfo.getMerchantId() != null){
            iotTerminalInfo.setMerchant(merchantService.getById(iotTerminalInfo.getMerchantId()));
            iotTerminalInfo.setMerchantFlag(true);
        }
        if(iotTerminalInfo.getCustomerId() != null){
            iotTerminalInfo.setCustomer(customerService.getById(iotTerminalInfo.getCustomerId()));
            iotTerminalInfo.setCustomerFlag(true);
            if(customerId.equals(iotTerminalInfo.getCustomer().getId())){
                iotTerminalInfo.setSelfFlag(true);
            }
        }
        return iotTerminalInfo;
    }
    public IPage<IotTerminalInfo> pageByCustomer(IotType iotType, Integer currentPage, Integer pageSize,Long customerId){
        IPage<IotTerminalInfo> pageList = null;
        Page<IotTerminalInfo> page = new Page<>(currentPage, pageSize);

        if(iotType == null){
            pageList = page(page, Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getCustomerId, customerId).orderByDesc(IotTerminalInfo::getId));
        }else{
            pageList = page(page, Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getCustomerId, customerId).eq(IotTerminalInfo::getIotType,iotType).orderByDesc(IotTerminalInfo::getId));
        }
        for(IotTerminalInfo iotTerminalInfo : pageList.getRecords()){
            iotTerminalInfo.setMerchantFlag(false);
            iotTerminalInfo.setCustomerFlag(false);
            iotTerminalInfo.setSelfFlag(false);
            if(iotTerminalInfo.getMerchantId() != null){
                iotTerminalInfo.setMerchant(merchantService.getById(iotTerminalInfo.getMerchantId()));
                iotTerminalInfo.setMerchantFlag(true);
            }
            if(iotTerminalInfo.getCustomerId() != null){
                iotTerminalInfo.setCustomer(customerService.getById(iotTerminalInfo.getCustomerId()));
                iotTerminalInfo.setCustomerFlag(true);
                if(customerId.equals(iotTerminalInfo.getCustomer().getId())){
                    iotTerminalInfo.setSelfFlag(true);
                }
            }

        }
        return pageList;
    }
    public IPage<IotTerminalInfo> pageByMerchant(Integer currentPage, Integer pageSize,Long merchantId){

        Page<IotTerminalInfo> page = new Page<>(currentPage, pageSize);
        IPage<IotTerminalInfo> pageList = page(page, Wraps.<IotTerminalInfo>lbQ().eq(IotTerminalInfo::getMerchantId, merchantId).orderByDesc(IotTerminalInfo::getId));
        JSONObject jsonObject = new JSONObject();
        for(IotTerminalInfo iotTerminalInfo : pageList.getRecords()){
            iotTerminalInfo.setMerchantFlag(false);
            iotTerminalInfo.setCustomerFlag(false);
            iotTerminalInfo.setSelfFlag(false);
            if(iotTerminalInfo.getMerchantId() != null){
                iotTerminalInfo.setMerchant(merchantService.getById(iotTerminalInfo.getMerchantId()));
                iotTerminalInfo.setMerchantFlag(true);
            }
            if(iotTerminalInfo.getCustomerId() != null){
                iotTerminalInfo.setCustomer(customerService.getById(iotTerminalInfo.getCustomerId()));
                iotTerminalInfo.setCustomerFlag(true);
            }
        }
        return pageList;
    }
}
